package master;

/**
 * @author chenyw
 * @date 2022/7/24 16:31
 */
public class M35searchInsert {
    public int searchInsert(int[] nums, int target) {
        int l = 0;
        int r = nums.length - 1;
        int mid = 0;
        while (l < r) {
            mid = (l + r) / 2;
            if (nums[mid] == target) {
                return mid;
            } else if (nums[mid] > target) {
                r = mid - 1;
            } else {
                l = mid + 1;
            }
        }
        return target > nums[l] ? l + 1 : l;
    }
}
